<?php

namespace app\admin\controller;

use app\admin\model\GradeRuleModel;
use cmf\controller\AdminBaseController;
use think\Request;

class GradeRuleController extends AdminBaseController
{

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        $rule = GradeRuleModel::all();
        $this->assign('rule', $rule);

        return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request, GradeRuleModel $model)
    {
        $data = $request->param();
        for ($i = 0; $i < count($data['login_day']); $i++) {

            $rule = ['login_day' => $data['login_day'][$i], 'coin' => $data['coin'][$i], 'grade' => $data['grade'][$i]];
            $res = $model->allowField(true)->create($rule);
        }

        $this->success('添加成功!');
    }


    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, GradeRuleModel $model)
    {
        $data = $request->param();

        for ($i = 0; $i < count($data['login_day']); $i++) {

            $rule = ['login_day' => $data['login_day'][$i], 'coin' => $data['coin'][$i]];
            $res = $model->allowField(true)->update($rule, ['id' => $data['id'][$i]]);
        }
        $this->success('保存成功!');
    }


}
